package com.yjd.comm.user.util;

import com.yjd.comm.bank.model.BankModel;
import com.yjd.comm.user.dto.ResidenceInfo;
import com.yjd.comm.user.dto.UserSession;
import com.yjd.comm.user.enums.ClientChannelEnum;
import com.yjd.comm.user.dto.ContactInfo;
import com.yjd.comm.user.model.UserDataModel;
import com.yjd.comm.user.model.UserModel;
import com.yjd.comm.user.dto.WorkInfo;
import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.util.StringUtil;
import com.yjd.comm.vo.CachedKeys;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName: UserUtil
 * @Description: 会员工具类
 * @author ready likun_557@163.com
 * @date 2014-8-16 下午3:01:41
 */
public class UserUtil {

	/**
	 * 获取登陆的会员信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public static UserSession getUserModel(HttpServletRequest request,
                                           HttpServletResponse response,
										   ClientChannelEnum clientChannelEnum) {

		UserSession userSession = (UserSession) request.getAttribute(CachedKeys.LOGIN_USER);
		if (clientChannelEnum == ClientChannelEnum.PIC) {
			if (userSession == null) {
				userSession = (UserSession) ServletUtil.getSession(request,
						response, CachedKeys.LOGIN_USER);
			}
		}
		return userSession;
	}

	public static void putSessionUserModel(HttpServletRequest request,
			HttpServletResponse response, UserModel userModel) {
		ServletUtil.putSession(request, response, CachedKeys.LOGIN_USER,
				userModel);
	}

	/**
	 * @Title: disposeIdCard
	 * @Description: 身份证号码显示处理
	 * @param @param idCard
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public static String disposeIdCard(String idCard) {
		if (StringUtil.isEmpty(idCard)) {
			return null;
		} else {
			if (idCard.length() > 6) {
				return idCard.substring(0, 6)
						+ StringUtil.replaceChar(idCard.substring(6), "*");
			} else {
				return idCard;
			}
		}
	}

	/**
	 * 
	 * 银行卡处理
	 * 
	 * @date 2015年11月20日 下午2:47:40
	 * @param bankCardNo
	 * @return String
	 */
	public static String disposeBankCardNo(String bankCardNo) {
		if (StringUtils.isBlank(bankCardNo)) {
			return null;
		}
		int length = bankCardNo.length() - 4;
		if (length <= 0) {
			return bankCardNo;
		}
		return StringUtil.replaceChar(bankCardNo.substring(0, length), "*")
				+ bankCardNo.substring(length);
	}

	/**
	 * 
	 * @Title: disposeEmail
	 * @Description: 邮件地址处理
	 * @return String
	 */
	public static String disposeEmail(String email) {
		if (StringUtils.isBlank(email)) {
			return email;
		}
		return StringUtils.replaceOnce(email,
				email.substring(2, email.indexOf("@")), "******");
	}

	/**
	 * 
	 * @Title: disposeMobile
	 * @Description: 手机号码处理
	 * @param mobile
	 * @return String
	 */
	public static String disposeMobile(String mobile) {
		if (StringUtils.isBlank(mobile)) {
			return mobile;
		}
		if (mobile.length() < 11) {
			return mobile;
		}
		String temp = mobile.substring(3);
		return mobile.substring(0, 3)
				+ StringUtils.replaceOnce(temp, temp.substring(0, 4), "****");
	}

	/**
	 * 处理用户名
	 * 
	 * @param userName
	 * @return
	 */
	public static String disposeUserName(String userName) {
		if (StringUtil.isNotEmpty(userName)) {
			StringBuilder builder = new StringBuilder();
			builder.append(userName.charAt(0));
			builder.append("***");
			if (userName.length() >= 2) {
				builder.append(userName.charAt(userName.length() - 1));
			}
			return builder.toString();
		} else {
			return null;
		}
	}

	/**
	 * 处理用户名
	 * 
	 * @param userName
	 * @return
	 */
	public static String disposeUserName1(String userName) {
		String um = disposeUserName(userName);
		if (StringUtil.isNotEmpty(um) && um.length() >= 2) {
			return um.substring(1);
		}
		return null;
	}

	public static String getDebtSign(UserModel debt_user,Long borrow_id){
		return DigestUtils.md5Hex(debt_user.getUser_id()+borrow_id+debt_user.getEncrypt()+debt_user.getPassword());
	}

	/**
	 * 用户工作信息是否录入， false 否 true 是
	 * @param workInfo
	 * @return
	 */
	public static boolean isJobInfoCompleted(WorkInfo workInfo){
		if(		null == workInfo ||
				StringUtil.isEmpty(workInfo.getCompany()) ||
				StringUtil.isEmpty(workInfo.getCompanyType()) ||
				StringUtil.isEmpty(workInfo.getCompanyLocation()) ||
				StringUtil.isEmpty(workInfo.getDuration()) ||
				StringUtil.isEmpty(workInfo.getResidenceLocation())
				){
			return false;
		}

		return true;
	}

	/**
	 * 用户联系人信息是否录入， false 否 true 是
	 * @param contactInfo
	 * @return
	 */
	public static boolean isContactInfoCompleted(ContactInfo contactInfo){
		if(		null == contactInfo ||
				StringUtil.isEmpty(contactInfo.getIdName()) ||
				StringUtil.isEmpty(contactInfo.getRelation()) ||
				StringUtil.isEmpty(contactInfo.getMobile_number()) ){
			return false;
		}

		return true;
	}

	/**
	 * 用户居住信息是否录入， false 否 true 是
	 *
	 * @return
	 */
	public static boolean isResidenceInfoCompleted(ResidenceInfo ri){
		if(		null == ri ||
				null == ri.getCurrentResidenceAddr() ||
				StringUtil.isEmpty(ri.getCurrentResidenceAddr().getCity()) ||
				StringUtil.isEmpty(ri.getCurrentResidenceAddr().getAddress()) ||
				null == ri.getFamilyAddr() ||
				StringUtil.isEmpty(ri.getFamilyAddr().getCity()) ||
				StringUtil.isEmpty(ri.getFamilyAddr().getAddress()) ||
				null == ri.getDomicileAddr() ||
				StringUtil.isEmpty(ri.getDomicileAddr().getCity()) ||
				StringUtil.isEmpty(ri.getDomicileAddr().getAddress())
				){
			return false;
		}

		return true;
	}

	/**
	 * 用户居住信息是否录入， false 否 true 是
	 *
	 * @return
	 */
	public static boolean isUrgentContactInfoCompleted(List<ContactInfo> urgentContacts){
		if( null == urgentContacts || 0 == urgentContacts.size() ){
			return false;
		}

		// 可以有多个联系人，至少有两个信息完整
		int counter = 0;
		for(ContactInfo ci: urgentContacts){
			if(UserUtil.isContactInfoCompleted(ci)){
				counter++;
			}
			if(counter >= 2 ){
				return true;
			}
		}

		return false;
	}

	/**
	 * 用户是否绑卡完成， false 否 true 是
	 *
	 * @return
	 */
	public static boolean isUserBindCardCompleted(BankModel userBankInfo){
		if( 	null == userBankInfo ||
				StringUtil.isEmpty(userBankInfo.getBank_card_number()) ||
				StringUtil.isEmpty(userBankInfo.getId_number()) ){
			return false;
		}

		return true;
	}

	/**
	 * 用户是否实名， false 否 true 是
	 *
	 * @return
	 */
	public static boolean isUserRealNameCompleted(UserDataModel userData){
		if( 	null == userData ||
				StringUtil.isEmpty(userData.getId_name()) ||
				StringUtil.isEmpty(userData.getId_number()) ){
			return false;
		}

		return true;
	}
}
